<template>
  <div class="template-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>考试模板管理</span>
          <el-button type="primary" @click="showCreateDialog">
            创建模板
          </el-button>
        </div>
      </template>

      <!-- 模板列表 -->
      <el-table :data="templateList" style="width: 100%">
        <el-table-column prop="name" label="模板名称" />
        <el-table-column prop="description" label="描述" show-overflow-tooltip />
        <el-table-column prop="duration" label="考试时长" width="100">
          <template #default="scope">
            {{ scope.row.duration }}分钟
          </template>
        </el-table-column>
        <el-table-column prop="totalScore" label="总分" width="80" />
        <el-table-column label="试题组成" width="200">
          <template #default="scope">
            <el-tooltip
              effect="dark"
              placement="top"
            >
              <template #content>
                <div v-for="item in scope.row.composition" :key="item.type">
                  {{ questionTypeMap[item.type] }}: {{ item.count }}题，共{{ item.score }}分
                </div>
              </template>
              <el-tag size="small">
                {{ scope.row.composition.length }}种题型
              </el-tag>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="250">
          <template #default="scope">
            <el-button-group>
              <el-button
                type="primary"
                size="small"
                @click="handleEdit(scope.row)"
              >
                编辑
              </el-button>
              <el-button
                type="success"
                size="small"
                @click="handleUse(scope.row)"
              >
                使用模板
              </el-button>
              <el-button
                type="danger"
                size="small"
                @click="handleDelete(scope.row)"
              >
                删除
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 模板编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'create' ? '创建模板' : '编辑模板'"
      width="60%"
    >
      <el-form
        ref="templateForm"
        :model="templateForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="模板名称" prop="name">
          <el-input v-model="templateForm.name" />
        </el-form-item>

        <el-form-item label="考试时长" prop="duration">
          <el-input-number
            v-model="templateForm.duration"
            :min="30"
            :max="180"
            :step="30"
          />
          <span class="unit">分钟</span>
        </el-form-item>

        <el-form-item label="总分" prop="totalScore">
          <el-input-number
            v-model="templateForm.totalScore"
            :min="0"
            :max="100"
          />
          <span class="unit">分</span>
        </el-form-item>

        <el-form-item label="试题组成">
          <div
            v-for="(item, index) in templateForm.composition"
            :key="index"
            class="composition-item"
          >
            <el-select v-model="item.type" placeholder="题型">
              <el-option
                v-for="(label, value) in questionTypeMap"
                :key="value"
                :label="label"
                :value="value"
              />
            </el-select>
            <el-input-number
              v-model="item.count"
              :min="1"
              placeholder="题数"
            />
            <el-input-number
              v-model="item.score"
              :min="1"
              placeholder="分数"
            />
            <el-button
              type="danger"
              circle
              @click="removeComposition(index)"
            >
              <el-icon><Delete /></el-icon>
            </el-button>
          </div>
          <div class="add-composition">
            <el-button type="primary" @click="addComposition">
              添加题型
            </el-button>
          </div>
        </el-form-item>

        <el-form-item label="描述" prop="description">
          <el-input
            v-model="templateForm.description"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">
            {{ dialogType === 'create' ? '创建' : '保存' }}
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Delete } from '@element-plus/icons-vue'

// 题型映射
const questionTypeMap = {
  single: '单选题',
  multiple: '多选题',
  blank: '填空题',
  programming: '编程题'
}

// 列表数据
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

const templateList = ref([
  {
    id: 1,
    name: 'C语言期中考试模板',
    description: '适用于C语言基础课程的期中考试',
    duration: 120,
    totalScore: 100,
    composition: [
      { type: 'single', count: 20, score: 40 },
      { type: 'multiple', count: 10, score: 30 },
      { type: 'programming', count: 2, score: 30 }
    ],
    createTime: '2024-04-01 10:00:00'
  }
])

// 对话框相关
const dialogVisible = ref(false)
const dialogType = ref('create')
const templateForm = reactive({
  name: '',
  duration: 120,
  totalScore: 100,
  composition: [],
  description: ''
})

const rules = {
  name: [
    { required: true, message: '请输入模板名称', trigger: 'blur' }
  ],
  duration: [
    { required: true, message: '请设置考试时长', trigger: 'change' }
  ],
  totalScore: [
    { required: true, message: '请设置总分', trigger: 'change' }
  ]
}

const showCreateDialog = () => {
  dialogType.value = 'create'
  Object.assign(templateForm, {
    name: '',
    duration: 120,
    totalScore: 100,
    composition: [],
    description: ''
  })
  dialogVisible.value = true
}

const handleEdit = (template) => {
  dialogType.value = 'edit'
  Object.assign(templateForm, {
    ...template
  })
  dialogVisible.value = true
}

const handleDelete = (template) => {
  ElMessageBox.confirm(
    '确定要删除该模板吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 实现删除逻辑
    ElMessage.success('删除成功')
  }).catch(() => {})
}

const handleUse = (template) => {
  // 跳转到创建考试页��，并使用该模板
  ElMessage.success('已应用模板')
}

const addComposition = () => {
  templateForm.composition.push({
    type: 'single',
    count: 1,
    score: 2
  })
}

const removeComposition = (index) => {
  templateForm.composition.splice(index, 1)
}

const handleSubmit = () => {
  // 实现提交逻辑
  dialogVisible.value = false
  ElMessage.success(dialogType.value === 'create' ? '创建成功' : '保存成功')
}

const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped>
.template-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.unit {
  margin-left: 10px;
  color: #909399;
}

.composition-item {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
  align-items: center;
}

.add-composition {
  margin-top: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style> 